package mianshi;

import niuke.TreeNode;

/**
 * description:
 * author:张腾
 * date:2021-07-21
 */
public class _04_06 {
    TreeNode pre = null;
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        if (root==null) return null;
        if (root.left!=null) {
            TreeNode node1 = inorderSuccessor(root.left, p);
            if (node1!=null) return node1;
        }
        if (pre==p) return root;
        else pre = root;
        if (root.right!=null) {
            TreeNode node2 = inorderSuccessor(root.right, p);
            if (node2!=null) return node2;
        }

        return null;
    }

//    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
//        if (root == null) return null;
//        if (root.val <= p.val) return inorderSuccessor(root.right, p);
//        TreeNode ans = inorderSuccessor(root.left, p);
//        return ans == null ? root : ans;
//    }

}
